package com.vdong.redisutil.distributionredis.service.impl;


import com.vdong.redisutil.distributionredis.service.SequenceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;


@Component("sequenceService")
public class SequenceServiceImpl implements SequenceService {
    private Logger log = LoggerFactory.getLogger(SequenceServiceImpl.class.getName());

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public SequenceServiceImpl() {
        log.info("--------------------------------------------SequenceServiceImpl()");
    }


    public Long generate(String key) {


        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.getAndAdd(1);

        long x1 = counter.longValue();


        return x1;

    }


    public Long generate(String key, int increment) {


        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.getAndAdd(increment);
        long x1 = counter.longValue();
        return x1;
    }


    public Long generateZero(String key) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        long x1 = counter.longValue();
        counter.getAndSet(0);

        return x1;

    }




}